package psn.kiko.controller.role;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.kiko.annotation.api.StableID;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.InterfaceDTO;
import psn.kiko.pojo.dto.RoleDTO;
import psn.kiko.pojo.entity.system.RoleEntity;
import psn.kiko.pojo.vo.DelDetailVO;
import psn.kiko.pojo.vo.LabelValue;
import psn.kiko.service.business.system.RoleService;

import java.util.List;

/**
 * 角色管理接口
 */
@RestController
@RequestMapping("/admin/rolemanagement")
@Api(tags = {"角色管理"})
public class RoleController{
	@Autowired
	RoleService roleService;
	
	@PostMapping("/pagingGetWithCondition/{pageNum}/{pageSize}")
	@ApiOperation("携带条件地分页查询角色列表")
	@StableID(101)
	public Result pagingGetWithCondition(
			@PathVariable("pageNum") @ApiParam("页码") int pageNum,
			@PathVariable("pageSize") @ApiParam("页面尺寸") int pageSize,
			@RequestBody @ApiParam("角色DTO") RoleDTO roleDTO
	){
		PageResult pageResult = roleService.pagingWithCondition(roleDTO,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@GetMapping("/allStatus")
	@ApiOperation("获取携带说明的状态所有值")
	@StableID(102)
	public Result allStatus(){
		List<LabelValue> statuses = roleService.getStatusesWithLabel();
		return Result.success(statuses);
	}
	
	@PostMapping("/add")
	@ApiOperation("添加角色")
	@StableID(103)
	public Result add(@RequestBody @ApiParam("角色DTO") RoleDTO roleDTO){
		Integer r = roleService.addRole(roleDTO);
		return Result.success(r);
	}
	
	@PostMapping("/batchAdd")
	@ApiOperation("批量添加角色")
	@StableID(104)
	public Result batchAdd(@RequestBody @ApiParam("角色DTO集合") List<RoleDTO> roleDTOs){
		Integer r = roleService.addRoles(roleDTOs);
		return Result.success(r);
	}
	
	@DeleteMapping("/batchDeleteByIds")
	@ApiOperation("根据主键批量删除角色")
	@StableID(105)
	public Result batchDeleteByIds(@RequestBody @ApiParam("角色id集合") List<Integer> ids){
		List<DelDetailVO> delDetailVOS = roleService.batchDeleteByRoleIds(ids);
		return Result.success(delDetailVOS);
	}
	
	@PutMapping("/updateRoleById")
	@ApiOperation("修改角色")
	@StableID(106)
	public Result updateRoleById(@RequestBody @ApiParam("含主键的角色对象") RoleDTO roleDTO){
		Integer r = roleService.updateRole(roleDTO);
		return Result.success(r);
	}
	
	@PutMapping("/batchEnableRole")
	@ApiOperation("批量启用角色")
	@StableID(107)
	public Result batchEnableRole(@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = roleService.enableRoleByIds(roleIds);
		return Result.success(r);
	}
	
	@PutMapping("/batchDisableRole")
	@ApiOperation("批量禁用角色")
	@StableID(108)
	public Result batchDisableRole(@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = roleService.disableRoleByIds(roleIds);
		return Result.success(r);
	}
	
	@GetMapping("/{roleId}")
	@ApiOperation("根据id获取角色")
	@StableID(109)
	public Result getRoleById(@PathVariable("roleId") @ApiParam("角色主键") Integer roleId){
		RoleEntity roleEntity = roleService.getRoleById(roleId);
		return Result.success(roleEntity);
	}
	
	//	-----------------------------------------------------------------API分配开始
	@PostMapping("/batchAddApisToRole/{roleId}")
	@ApiOperation("给角色添加一批接口")
	@StableID(113)
	public Result batchAddApisToRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@RequestBody @ApiParam("接口稳定id集合") List<Integer> apiStableIds){
		Integer r = roleService.batchAddApisToRole(roleId,apiStableIds);
		return Result.success(r);
	}
	
	@PostMapping("/getAllocatedApisOfRole/{roleId}/{pageNum}/{pageSize}")
	@ApiOperation("分页获取角色已分配的接口")
	@StableID(110)
	public Result getAllocatedApisOfRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@PathVariable("pageNum") @ApiParam("页码") int pageNum,
			@PathVariable("pageSize") @ApiParam("页面尺寸") int pageSize,
			@RequestBody @ApiParam("接口DTO") InterfaceDTO interfaceDTO){
		PageResult pageResult = roleService.getAllocatedApisOfRole(pageNum,pageSize,roleId,interfaceDTO);
		return Result.success(pageResult);
	}
	
	@PostMapping("/getUnallocatedApisOfRole/{roleId}/{pageNum}/{pageSize}")
	@ApiOperation("分页获取角色还未分配到的接口")
	@StableID(111)
	public Result getUnallocatedApisOfRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@PathVariable("pageNum") @ApiParam("页码") int pageNum,
			@PathVariable("pageSize") @ApiParam("页面尺寸") int pageSize,
			@RequestBody @ApiParam("接口DTO") InterfaceDTO interfaceDTO){
		PageResult pageResult = roleService.getUnallocatedApisOfRole(pageNum,pageSize,roleId,interfaceDTO);
		return Result.success(pageResult);
	}
	
	@DeleteMapping("/batchDeleteApisOfRole/{roleId}")
	@ApiOperation("删除角色的一批接口")
	@StableID(112)
	public Result batchDeleteApisOfRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@RequestBody @ApiParam("接口稳定id集合") List<Integer> apiStableIds){
		Integer r = roleService.batchDeleteApisOfRole(roleId,apiStableIds);
		return Result.success(r);
	}
	
	@PutMapping("/batchDisableApisOfRole/{roleId}")
	@ApiOperation("批量禁用角色的一批接口")
	@StableID(114)
	public Result batchDisableApisOfRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@RequestBody @ApiParam("接口稳定id集合") List<Integer> apiStableIds){
		Integer r = roleService.batchDisableApisOfRole(roleId,apiStableIds);
		return Result.success(r);
	}
	
	@PutMapping("/batchEnableApisOfRole/{roleId}")
	@ApiOperation("批量启用角色的一批接口")
	@StableID(115)
	public Result batchEnableApisOfRole(
			@PathVariable("roleId") @ApiParam("角色id") int roleId,
			@RequestBody @ApiParam("接口稳定id集合") List<Integer> apiStableIds){
		Integer r = roleService.batchEnableApisOfRole(roleId,apiStableIds);
		return Result.success(r);
	}
	//	-----------------------------------------------------------------API分配结束
}
